﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class Shop_HelpType_Edit : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ReadAction();               //保存传过来的参数  
            LoadGoodType();
        }
    }

    #region Web 窗体设计器生成的代码

    protected override void OnLoad(EventArgs e)
    {
        isRequestLogin = true;                                  //是否需要登录
        RequestLoginPage = this.Request.Url.AbsoluteUri;        //获取决对Url
        isAtFramePageLogin = true;                              //是框架页

        base.OnLoad(e);
    }

    #endregion

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("HelpType_Manager.aspx");
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        int ID = Shove._Convert.StrToShort(hID.Value, 0);
        DAL.Tables.tbl_Site_HelpType table = new DAL.Tables.tbl_Site_HelpType();

        table.TypeName.Value = tbNewType.Text;
        table.ParentID.Value = ddlType.SelectedValue;
        table.Status.Value = Shove._Convert.StrToShort(rblTypeStatus.SelectedValue,0);
        table.Introduce.Value = txtIntroduce.Text;
        table.ShopID.Value = _Site.ID;

        if (tbNewType.Text.Equals(""))
        {
            Shove._Web.JavaScript.Alert(this.Page, "类别名称不能为空！");
            return;
        }

        if (this.hAction.Value == "Edit")           //修改
        {
            long i = table.Update("[ID]=" + ID);

            if (i >= 0)
            {
                Shove._Web.JavaScript.Alert(this.Page, "修改类别成功！", "HelpType_Manager.aspx");
            }
            else
            {
                Shove._Web.JavaScript.Alert(this.Page, "修改类别失败！");
            }
        }
        if (this.hAction.Value == "Add")            //添加
        {            
            long i = table.Insert();

            if (i >= 0)
            {
                Shove._Web.JavaScript.Alert(this.Page, "添加类别成功！", "HelpType_Manager.aspx");
            }
            else
            {
                Shove._Web.JavaScript.Alert(this.Page, "添加类别失败！");
            }
        }
    }

    /// <summary>
    /// 梆定商品类别
    /// </summary>
    private void LoadGoodType()
    {
        DataTable dt = new DAL.Tables.tbl_Site_HelpType().Open("", "ParentID=0", "");

        ddlType.DataSource = dt;
        ddlType.DataTextField = "TypeName";
        ddlType.DataValueField = "ID";
        ddlType.DataBind();
        ddlType.Items.Insert(0, new ListItem("--无--", "0"));
    }

    /// <summary>
    /// 保存传过来的参数
    /// </summary>
    private void ReadAction()
    {
        this.hAction.Value = Shove._Web.Utility.GetRequest("Action");
        this.hID.Value = Shove._Web.Utility.GetRequest("ID");

        if (this.hAction.Value == "Edit")
        {
            BindData();
        }
        if (this.hAction.Value == "Add")
        {
            LoadGoodType();     //梆定所有父类别
        }
    }

    /// <summary>
    /// 编辑状态时，初始化
    /// </summary>
    private void BindData()
    {
        int ID = Shove._Convert.StrToInt(hID.Value, 0);

        DataTable dt = new DAL.Tables.tbl_Site_HelpType().Open("", "ID=" + ID, "");

        if (dt != null)
        {
            tbNewType.Text = dt.Rows[0]["TypeName"].ToString();            
            rblTypeStatus.SelectedValue = dt.Rows[0]["Status"].ToString();

            if (dt.Rows[0]["ParentID"].ToString() != "0")
            {
                ddlType.SelectedValue = dt.Rows[0]["ParentID"].ToString();
            }

            txtIntroduce.Text = dt.Rows[0]["Introduce"].ToString();            
        }
    }

}
